<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form label-position="right" :model="enterpriseForm" :label-width="formLabelWidth" ref="enterpriseFormRef" :rules="rules">
        <el-row>
          <el-col :span="11">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="enterpriseForm.name" placeholder="请输入企业名称" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="成立日期" prop="establishmentDate">
              <el-date-picker
                  v-model="enterpriseForm.establishmentDate" type="date" placeholder="成立日期" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="enterpriseForm.registeredAddress" placeholder="请输入注册地址" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="enterpriseForm.contactPhone" placeholder="请输入联系电话" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="enterpriseForm.email" placeholder="请输入邮箱" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="官网网址" prop="officialWebsite">
              <el-input v-model="enterpriseForm.officialWebsite" placeholder="请输入官网网址" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="纳税识别号" prop="taxIdentificationNumber">
              <el-input v-model="enterpriseForm.taxIdentificationNumber" placeholder="请输入纳税识别号" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import {defineEmits, defineProps, inject, onMounted, reactive, ref, toRefs} from 'vue';
import enterpriseApi from '@/api/enterpriseApi'
import type {FormInstance} from 'element-plus';
import {ElMessage} from 'element-plus'
import type {EnterpriseForm} from "@/types/req/enterpriseForm";
import type {Result} from '@/types/result'
import type {Enterprise} from "@/types/resp/enterprise";
import type {Id} from "@/types/id";

const props = defineProps<Id>()
const enterpriseFormRef = ref<FormInstance | null>(null);
const enterpriseForm = reactive<EnterpriseForm>({
  name: '',
  establishmentDate: '',
  registeredAddress: '',
  contactPhone: '',
  email: '',
  officialWebsite: '',
  taxIdentificationNumber: '',
})
const emit = defineEmits<{
  (e: 'resetEnterpriseAddEvent'): void;
  (e: 'closeEnterpriseAddEvent', data?: { search: boolean }): void;
}>()

const rules = reactive({
  name: [
    { required: true, message: '请输入企业名称', trigger: 'blur' }
  ],
  registeredAddress: [
    { required: true, message: '请输入注册地址', trigger: 'blur' }
  ],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' }
  ],
  taxIdentificationNumber: [
    { required: true, message: '请输入纳税识别号', trigger: 'blur' }
  ],
});
const dialogTop = ref<string>('')
const dialogWidth = ref<string>('')
const formLabelWidth = ref<string>('')

onMounted(() => {
  if (props.id) {
    initEnterpriseFormById(props.id)
  }
  const globalState = inject('globalState', {} as { dialogWidth?: string, dialogTop?: string, formLabelWidth?: string })
  if (!globalState) {
    throw new Error('globalState is not provided');
  }
  const globalStateRefs = toRefs(globalState)
  if (globalStateRefs.formLabelWidth) {
    formLabelWidth.value = globalStateRefs.formLabelWidth.value || ''
  }
  if (globalStateRefs.dialogWidth) {
    dialogWidth.value = globalStateRefs.dialogWidth.value || ''
  }
  if (globalStateRefs.dialogTop) {
    dialogTop.value = globalStateRefs.dialogTop.value || ''
  }
});

const initEnterpriseFormById = async (id: number) => {
  const resp: Result<Enterprise> = await enterpriseApi.findById(id)
  if (resp && resp.code === 1 && resp.data) {
    Object.assign(enterpriseForm, resp.data)
  }
}

const onReset = () => {
  if (enterpriseFormRef.value) {
    enterpriseFormRef.value.resetFields()
  }
  emit('resetEnterpriseAddEvent')
}

const onSave = () => {
  if (!enterpriseFormRef.value) {
    return
  }
  enterpriseFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      const resp: Result<void> = await enterpriseApi.save(enterpriseForm)
      if (resp && resp.code === 1) {
        ElMessage.success('保存成功')
        emit('closeEnterpriseAddEvent', {search: true})
      } else {
        ElMessage.error('保存失败:' + (resp.msg ? resp.msg : ''))
      }
    }
  })
}

const onBack = () => {
  emit('closeEnterpriseAddEvent')
}
</script>

<style lang="scss">
</style>
